class ShangpinsController < ApplicationController
  # GET /shangpins
  # GET /shangpins.xml
  def daoru
require 'hpricot'
require 'open-uri'

	@doc=""
   @doc=Hpricot(Iconv.iconv("UTF-8//IGNORE","euc-kr//IGNORE",open("http://sampleshop.co.kr/template/1/categ_list.asp?categ_code=16&page=1&cv=6&orderOPT=0&pagesize=612").read).to_s)
   @addr=""
   @doc=(@doc/"table[@width='95%']")
   @doc.search("/tr[2]").remove
   @doc.search("tr:eq(1)").remove
   shuliang=Array.new
   jiage=Array.new
   imgsrc=Array.new
   mail=''
   mail1=''
   i=0
   @doc.search("td:eq(3) div").each do |item|#shuliang
     shuliang[i]=item
     i=i+1
   end
   i=0
   @doc.search("td:eq(2)").each do |item|#jiage
     jiage[i]=item
     i=i+1
   end
   i=0
   @doc.search("td:eq(0) a img").each do |item|#img
     imgsrc[i]='http://sampleshop.co.kr'+item['src'].to_s
     i=i+1
   end
   i=0
   @doc.search("td:eq(1) a").each do |item|#href
     imgurl='http://sampleshop.co.kr'+item['href'].to_s
     jiage1=jiage[i+1].to_s.scan(/\d+/)
     shuliang1=shuliang[i].to_s.scan(/\d+/).to_s
     unless Shangpin.find(:first, :conditions => "url = '#{imgurl}'")
         d=Shangpin.new()
         d.url=imgurl
         d.tupian=imgsrc[i]
         d.jiage=jiage1
         d.biaoti=item.innerText.to_s
         d.shuliang=shuliang1
         d.save
         mail=mail+"<a href=#{d.url}><img src=#{d.tupian}><br>#{d.biaoti} #{d.jiage} #{d.shuliang}</a><br>"
      else
        shangpin=Shangpin.find(:first, :conditions => "url = '#{imgurl}'")
         shangpin.laojiage=shangpin.jiage
         shangpin.jiage=jiage1
         shangpin.save
        if shangpin.jiage<shangpin.laojiage
         mail=mail+"<a href=#{shangpin.url}><img src=#{shangpin.tupian}><br>#{shangpin.biaoti} #{shangpin.jiage}降价 #{shangpin.laojiage}#{shangpin.shuliang}</a><br>"
        end

      end
         i=i+1
     end
     if mail.length>0 
       Mymail.deliver_send("aontimer@gmail.com",mail)
     end
  end
  def show

    @yonghu = Yonghu.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @yonghu }
    end
  end

  def index

    @shangpins =Shangpin.paginate:page=>params[:page],:per_page=>50
flash[:notice] = session[:yonghu]
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @shangpins }
    end
  end
def gouwuche

 if params[:shangpin]!=nil
  	session[:gouwuche] .collect {|x|
		if x.id== params[:shangpin][:id].to_i
		   x.shuliang=params[:shangpin][:shuliang]
		end
		}
 else
	if session[:gouwuche]==nil
		session[:gouwuche] = Array.new
	end
	addshangpin=Shangpin.find(params[:id])

	if session[:gouwuche] .include? addshangpin
		session[:gouwuche] .collect {|x|
		if x== addshangpin
		   x.shuliang=x.shuliang.to_i+1
		end
		}
	else
	shangpin=Shangpin.find(params[:id])
	shangpin.shuliang=1
	 	session[:gouwuche] <<shangpin
	 end
  end
end

  def gouwuche2
 	session[:gouwuche] .collect {|x|
		if x.id== params[:shangpin][:id]
		@a=params[:shangpin][:shuliang]
		   x.shuliang=params[:shangpin][:shuliang]
		end
		}
   end
  # GET /shangpins/1
  # GET /shangpins/1.xml
  def show
    @shangpin = Shangpin.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @shangpin }
    end
  end

  # GET /shangpins/new
  # GET /shangpins/new.xml
  def new
    @shangpin = Shangpin.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @shangpin }
    end
  end

  # GET /shangpins/1/edit
  def edit
    @shangpin = Shangpin.find(params[:id])
  end

  # POST /shangpins
  # POST /shangpins.xml
  def create
    @shangpin = Shangpin.new(params[:shangpin])

    respond_to do |format|
      if @shangpin.save
        flash[:notice] = 'Shangpin was successfully created.'
        format.html { redirect_to(@shangpin) }
        format.xml  { render :xml => @shangpin, :status => :created, :location => @shangpin }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @shangpin.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /shangpins/1
  # PUT /shangpins/1.xml
  def update
    @shangpin = Shangpin.find(params[:id])

    respond_to do |format|
      if @shangpin.update_attributes(params[:shangpin])
        flash[:notice] = 'Shangpin was successfully updated.'
        format.html { redirect_to(@shangpin) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @shangpin.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /shangpins/1
  # DELETE /shangpins/1.xml
  def destroy
    @shangpin = Shangpin.find(params[:id])
    @shangpin.destroy

    respond_to do |format|
      format.html { redirect_to(shangpins_url) }
      format.xml  { head :ok }
    end
  end
end
